package com.vmladenov.cook.ui.user;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;

import com.vmladenov.cook.R;
import com.vmladenov.cook.core.DialogHelper;
import com.vmladenov.cook.core.db.UserServiceHelper;
import com.vmladenov.cook.ui.MainActivity;

public final class LoginActivity extends Activity
{
	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.login_screen);
	}

	public void onLogin(View v)
	{
		EditText tbUserName = (EditText) findViewById(R.id.tbUserName);
		EditText tbPassWord = (EditText) findViewById(R.id.tbPassword);

		String password = tbPassWord.getText().toString();
		String userName = tbUserName.getText().toString();

		UserServiceHelper userServiceHelper = new UserServiceHelper();
		long userId = userServiceHelper.loginUser(userName, password);
		if (userId == -1)
		{
			DialogHelper.showErrorDialog(this, getString(R.string.wrongPasswordOrUserName));
			return;
		}
		SharedPreferences preferences = getSharedPreferences("MandjaSettings", MODE_PRIVATE);
		Editor editor = preferences.edit();
		editor.putLong("UserId", userId);
		editor.putString("UserName", userName);
		editor.commit();
		Intent intent = new Intent();
		intent.setClass(LoginActivity.this, MainActivity.class);
		startActivity(intent);
	}
}
